package com.ding.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * LoginController
 *
 * @author DYS
 * @date 2021/7/20
 * @time 22:29
 * @Description
 */
@Controller
public class LoginController {

	//登录
	@RequestMapping("/user/login")
	public String login(@RequestParam("username") String username,
						@RequestParam("password") String password,
						Model model, HttpSession session) {
		//进行登录验证
		if (username != null && "123456".equals(password)) {
			session.setAttribute("username",username);
			return "redirect:/main.html";
		} else {
			//告诉用户用户名或密码错误
			model.addAttribute("msg", "用户名或密码不正确");
			return "index";
		}
	}

	//退出
	@RequestMapping("/user/logout")
	public String logout(HttpSession session){
		session.removeAttribute("username");
		session.invalidate();
		return "redirect:/index.html";
	}
}
